package com.UI;

import java.util.ArrayList;

import com.Model.StudentDBModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class EditClassEvent extends Activity{
	private String eventType;
	private String comboID;
	private StudentDBModel model;
	private ArrayList classEventDetails;
	private String[] eventDetails;
	private ContentValues editedValues;
	private int dbEventType;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_class_event);
		
		Bundle extras = getIntent().getExtras();
		comboID = extras.getString("EventCourseComboID");
		model = new StudentDBModel(this);
		classEventDetails = model.getClassEventDetails(comboID);
		eventDetails = (String[]) classEventDetails.toArray(new String[classEventDetails.size()]);
		
		//create the spinner to hold the list of Class events that can be added
		Spinner classEventSpinner = (Spinner) findViewById(R.id.addClassEventSpinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.AddClassEventArray, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		classEventSpinner.setAdapter(adapter);
		//set the spinner to the type retrieved from the DB
		if(eventDetails[3].compareTo("Assignment") == 0){
			dbEventType = 1;
			eventType = "Assignment";
		}
		else if(eventDetails[3].compareTo("Project") == 0){
			dbEventType = 2;
			eventType = "Project";
		}
		else if(eventDetails[3].compareTo("Exam") == 0){
			dbEventType = 3;
			eventType = "Exam";
		}
		else{
			dbEventType = 0;
			eventType = "";
		}
		classEventSpinner.setSelection(dbEventType);

		// Set up a call back for the spinner in case it gets changed
		classEventSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onNothingSelected(AdapterView<?> arg0) {
				// Do nothing.
			}

			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				final Spinner selectedEvent = (Spinner) findViewById(R.id.addClassEventSpinner);
				eventType = selectedEvent.getSelectedItem().toString();
			}
		});
		
		TextView lblEventNameFromDB = (TextView) findViewById(R.id.lblEventNameFromDB);
		lblEventNameFromDB.setText(eventDetails[1]);
		
		EditText txtClassEventWeight = (EditText) findViewById(R.id.editClassEventWeight);
		txtClassEventWeight.setText(eventDetails[6]);
		
		EditText txtClassEventDueDate = (EditText) findViewById(R.id.editClassEventDueDate);
		txtClassEventDueDate.setText(eventDetails[7]);
		
		EditText txtMarkReceived = (EditText) findViewById(R.id.editMarkReceived);
		txtMarkReceived.setText(eventDetails[4]);
		
		EditText txtOutOf = (EditText) findViewById(R.id.editOutOf);
		txtOutOf.setText(eventDetails[5]);
		
		// register this activity as a button event listener
        Button btnSubmit = (Button)findViewById(R.id.btnClassEventSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		
        		if(eventType.length() == 0){
			 		   AlertDialog alertDialog = new AlertDialog.Builder(EditClassEvent.this).create();	        			
			 		   alertDialog.setTitle("Warning");	        		  
			 		   alertDialog.setMessage("Please select a Class Event Type");	        		  	        		  
			 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			 		  
			 		   public void onClick(DialogInterface dialog, int which) {
					       return;
					  } }); 
					  alertDialog.show();
					
				}
				
				else {
        		// collect the form data
        		editedValues = new ContentValues();
        		
        		editedValues.put("EventType", eventType);

        		EditText txtClassEventWeight = (EditText) findViewById(R.id.editClassEventWeight);
        		editedValues.put("EventWeight", txtClassEventWeight.getText().toString());
        		
        		EditText txtClassEventDueDate = (EditText) findViewById(R.id.editClassEventDueDate);
				editedValues.put("DueDate", txtClassEventDueDate.getText().toString());
				
				EditText txtMarkReceived = (EditText) findViewById(R.id.editMarkReceived);				
				
				if (txtMarkReceived.getText().toString().equals("")){
					editedValues.put("MarkReceived", 0);
				}
				else {
					editedValues.put("MarkReceived", txtMarkReceived.getText().toString());
				}
				
				EditText txtOutOf = (EditText) findViewById(R.id.editOutOf);
				if (txtOutOf.getText().toString().equals("")){
					editedValues.put("OutOf", 0);
				}
				else {
					editedValues.put("OutOf", txtOutOf.getText().toString());
				}

	    		// use the model to store the course data
	    		model.updateClassEvent(editedValues, comboID);
	    		
	    		TextView lblResults = (TextView) findViewById(R.id.lblEditClassEventResult);
				lblResults.setText(getString(R.string.classEventEdited));

				//finish the activity to return to the calling event
				EditClassEvent.this.finish();	
				
				}
        	}           
        });
        
     // register this activity as a button event listener
		Button btnGoBack = (Button) findViewById(R.id.btnBack);
		btnGoBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go back to the pick class event view
				EditClassEvent.this.finish();
			}
		});
		
		Button btnGoHome = (Button) findViewById(R.id.btnHome);
		btnGoHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent goHome = new Intent(EditClassEvent.this, HomePage.class);
				startActivity(goHome);
				//finish the activity to return to the calling event
				EditClassEvent.this.finish();
			}
		});
	}
}
